$.extend({
	target:function(event){
		return $.browser.msie?event.srcElement:event.target;
	},
	showload:function(){
		var loading=$('.pageloading'),left=($(window).width()/2-64),top=$(window).height()/2-64+$(window).scrollTop();
		if(loading.length==1){
			loading.css('left',left);loading.css('top',top);
			loading.show();
		}else{
			loading=$('<div class="pageloading" style="left:'+left+'px;top:'+top+'px;"></div>');
			$(document.body).append(loading);
			loading.click($.hideload);
		}
	},
	hideload:function(){
		$('.pageloading').hide();
	}
});
$(document).ready(function(){
	$('input[type="text"],input[type="password"]').focus(function(){
		var loadclass='border';
		if($(this).attr('search')){loadclass+=' wipe';}
		$(this).addClass(loadclass);
	}).blur(function(){
		var move='border';
		if($(this).attr('search')){if($.trim($(this).attr('value')).length==0){move+=' wipe';}}
		$(this).removeClass(move);
	});
	$('input[type="submit"],input[type="reset"]').mouseover(function(){$(this).addClass('hover');}).mouseout(function(){$(this).removeClass('hover');});
	$('input[type="checkbox"][name="all"]').click(function(){
		$(this).attr('checked')?$('input[type="checkbox"]').attr('checked',true):$('input[type="checkbox"]').attr('checked',false);
	});
	$('div.close').click(function(){
		$(this).parent().fadeOut('normal');
	});
	$('a[fancybox]').each(function(){
		eval('var init={'+$(this).attr('fancybox')+'};');
		init=$.extend({'padding':0,'overlayColor':'#2A2A2A','autoScale':false,'transitionIn':'none','transitionOut':'none','type':'iframe','transitionIn':'elastic','transitionOut':'elastic'},init);
		$(this).fancybox(init);
	});
	$('a[action]').click(function(){
		var func=$(this).attr('action');
		if(window[func]){
			var mixed=$(this).attr('mixed');
			if(mixed){
				eval('mixed={'+mixed+'};');
				(window[func])(this,mixed);
			}else{
				(window[func])(this);
			}
		}
		return false;
	});
	$('td.ac>div').mouseover(function(){
		$(this).toggleClass('hover');
	}).mouseout(function(){
		$(this).toggleClass('hover');
	});
	InitSelect();
});
function InitSelect(expr){
	if(typeof expr !=='string'){expr='';}
  	$('div.select'+expr+'>a').click(function(){
		$('div.select>.box,div.select>ul').css('display','none');
		var select=$(this).parents('.select'),obj=$(this).next();
		obj.css('display','block');
		$(document.body).click(function(event){
			if(!$.contains((select)[0],$.target(event))){
				if(!select.attr('lock')){
					var hide=select.attr('hide'),display='none';
					if(typeof hide==='string'){
						if((window[hide])(select)){
							display='block';
						}
					}
					obj.css('display',display);
				}
				$(document.body).unbind('click');
			}
		});
		return false;
	});
	$('div.select'+expr+' > ul > li').each(function(){
		var copy=function(obj){
			$(obj).parent('ul').prev('a').children('label').text($(obj).text());
			$(obj).parent('ul').prev('a').children('input').attr('value',$(obj).attr('tab'));			
		}
		if($(this).attr('selected')){copy(this);}
		$(this).click(function(){
			copy(this);$(this).parent('ul').css('display','none');
			var change=$(this).parents('.select').attr('change');
			if(typeof change==='string'){(window[change])(this);}
			var href=$(this).children('a').attr('href');
			if(href=='#'||href==''){return false;}
		});
	});	
}
function AjaxError(X,T,E){
	alert('网络错误!');
}
function RequestPost(name,func){
	var form=$('form[name="'+name+'"]');
	var submit=$('input[type="submit"]');
	var fields=form.serializeArray();
	fields.push({'name':'save','value':1});
	if(typeof func==='string'){
		if(window[func]){if(!((window[func])())){return false;}}
	}
	$.ajax({type:'POST',url:form.attr('action'),data:fields,dataType:'json',
	error:function(){
		$.showload();
		submit.attr('disabled',false).attr('value',submit.data('cache'));
		AjaxError();
	},
	success:function(json){
		$.hideload();
		if(json.msg.length!=0){form.find('input[type="submit"]').attr('disabled',false);alert(json.msg);}
		if(json.result==1){parent.$.fancybox.close();}
		if(json.result==2){window.top.location.reload();}
		submit.attr('disabled',false).attr('value',submit.data('cache'));
	}
	});
	submit.data('cache',submit.attr('value'));
	submit.attr('disabled',true).attr('value','加载中');
	return false;
}
function RequestA(target,mixed){
	mixed=$.extend({confirm:false,sub:{}},mixed);
	var obj=$(target),tr=obj.parents('tr'),td=tr.find('td'),load=td.first();
	if(mixed.confirm){if(!window.confirm('是否确定要进行'+obj.text()+'操作?')){return false;}}
	load.data('cache',load.html());load.empty();load.addClass('loading');
	$.ajax({type:'GET',url:obj.attr('href'),data:mixed.sub,dataType:'json',error:function(){
			AjaxError();load.removeClass('loading');load.html(load.data('cache'));
		},
		success:function(json){
			load.removeClass('loading');load.html(load.data('cache'));
			if(json.msg.length!=0){alert(json.msg);}
			if(json.result==1){tr.fadeOut("slow");}
			if(json.result==2){window.top.location.reload();}
			if(json.result==5){
				if(json.td){for(key in json.td.data){var temp=td.get(json.td.offset++);if(temp!=null){$(temp).text(json.td.data[key]);}}}
				for(key in json.exec){
					if(obj[key]){
						if(typeof json.exec[key]==='string'){
							(obj[key])(json.exec[key]);
						}else if(typeof json.exec[key]==='object'){
							for(k in json.exec[key]){
								(obj[key])(k,json.exec[key][k]);
							}
						}	
					}
				}
			}
		}
	});
}
function RequestBa(target,mixed){
	var obj=$(target),choose=$('td>input:checked'),current=0;
	if(choose.length==0){
		alert('未选择任何一行数据.');
		return false;
	}
	var funcPa=function(){
		var sub=[];
		choose.each(function(){
			sub.push({'name':$(this).attr('name'),'value':$(this).attr('value')});
		});
		$.ajax({type:'GET',url:obj.attr('href'),data:sub,dataType:'json',error:AjaxError,
			success:function(json){
				if(json.msg.length!=0){alert(json.msg);}
				if(json.result==2){window.top.location.reload();}
			}
		});
	},funcSi=function(){
		var input=$(choose.get(current++));
		if(input.length==0){return false;}
		tr=input.parents('tr'),td=tr.find('td'),load=td.first();
		load.data('cache',load.html());load.empty();load.addClass('loading');
		eval('var send={'+input.attr('name').replace(/\[\]/g,'')+':\''+input.attr('value')+'\'}');
		$.ajax({type:'GET',url:obj.attr('href'),data:send,dataType:'json',error:function(){
				AjaxError();load.removeClass('loading');load.html(load.data('cache'));
			},
			success:function(json){
				load.removeClass('loading');load.html(load.data('cache'));
				if(json.result==1){tr.fadeOut("slow");}
				if(json.result==5){
					if(json.td){for(key in json.td.data){var temp=td.get(json.td.offset++);if(temp!=null){$(temp).text(json.td.data[key]);}}}
					for(key in json.exec){
						if(obj[key]){
							if(typeof json.exec[key]==='string'){
								(obj[key])(json.exec[key]);
							}else if(typeof json.exec[key]==='object'){
								for(k in json.exec[key]){
									(obj[key])(k,json.exec[key][k]);
								}
							}	
						}
					}
				}
				funcSi();
			}
		});
	};
	if(typeof mixed=='object'){if(!mixed.exec){mixed.exec='funcPa';}
	}else{mixed={'exec':'funcPa'};}
	if(window.confirm('是否确定要批量'+obj.text()+'操作?')){
		try{eval('('+mixed.exec+')();');}catch(e){alert(e);}
	}
}
function RequestLd(target,mixed){
	var obj=$(target);
	mixed=$.extend({confirm:false,sub:{}},mixed);
	if(mixed.confirm){if(!window.confirm('是否确定要进行'+obj.text()+'操作?')){return false;}}
	$.showload();
	$.ajax({type:'GET',url:obj.attr('href'),dataType:'json',error:AjaxError,
		success:function(json){
			if(json.msg.length!=0){alert(json.msg);}
			$.hideload();
		}
	});
}
function ChartLoad(a){
	if($(a).is('a[chart]')){
		$('a[chart][class="k"]').removeClass();
		$(a).filter('a[chart]').addClass('k');
	}
	var pams={},date=$('.date').text().split(' -- ');
	pams['start']=date[0];pams['end']=date[1];
	pams['metrics_chart']=$('input[name="metrics_chart"]').attr('value');
	pams['contrast']=$('input[name="contrast"]').attr('value');
	pams['type']=$('a[chart][class="k"]').attr('chart');
	$.showload();
	$.get('index.php?c=flux&a=index&action=chart',pams,function(json){
		var chart=FindSwf('chart');
		chart.load(json);
		$.hideload();
	},'text');
}
function TableLoad(a){
	$.showload();
	var url=a,input=null;
	$('input:checked').each(function(){
		input=$(this);url+='&'+input.attr('name')+'='+input.attr('value');
	});
	$.get(url,{},function(json){
		$('#table').html(json.html);
		FluxInit();
		$.hideload();
	},'json');
	return false;
}
function DimMet(select){
	var T=new Array();
	$('input:checked').each(function(i){
		T[i]=$(this).attr('value');
	});
	if(select.attr('cache')!=T.join(',')){
		select.attr('cache',T.join(','));
		var date=$('.date').text().split(' -- ');
		TableLoad('index.php?c=flux&a=index&action=table'+'&start='+date[0]+'&end='+date[1]);
	}
}
function FluxInit(){
	$('.fix>thead>tr>th>a').click(function(){
		var href=$(this).attr('href');
		TableLoad(href);
		return false;
	});
	$('.pagination>a').click(function(){
		var href=$(this).attr('href');
		TableLoad(href);
		return false;
	});
}
function FindSwf(tar) {
  if(navigator.appName.indexOf("Microsoft")!= -1){
    return window[tar];
  }else{
    return document[tar];
  }
}